<template>
  <div>
    <van-nav-bar title="面经注册"/>
    <van-form @submit="onSubmit">
  <van-field
  autocomplete="off"
    v-model="username"
    name="username"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' },{ pattern:/[a-zA-Z0-9\u4e00-\u9fa5_]{5,10}/, message: '请输入5-10位的用户名' }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' },{ pattern:/[a-zA-Z0-9]{6,15}/, message: '请输入6-15位的密码' }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">注册</van-button>
  </div>
</van-form>
  <div class="goLogin"><a @click="$router.push('/login')">有账号，去登录</a></div>
  </div>
</template>

<script>
import { register } from '@/api/user'
import { Toast } from 'vant'
export default {
  name: 'RegisterPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      // console.log('submit', values)
      await register(values)
      Toast('注册成功')
      setTimeout(() => {
        this.$router.push('/login')
      }, 1500)
    }
  }
}
</script>

<style lang="less" scoped>
.van-button{
  background-color: #07c160;
  border: #07c160 1px solid;
  border-radius: 3px;
}
.goLogin{
  color: #006699;
  text-align: right;
  padding-right: 20px;
}
</style>
